package com.nmf.site.biz.compcollege.comp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.nmf.dao.gen.BaseCategoryMapper;
import com.nmf.dao.gen.BaseCollegeMapper;
import com.nmf.dao.gen.BizCompCollegeMapper;
import com.nmf.dao.gen.BizCompanyMapper;
import com.nmf.dao.gen.bean.BaseCategory;
import com.nmf.dao.gen.bean.BaseCollege;
import com.nmf.dao.gen.bean.BaseCollegeExample;
import com.nmf.dao.gen.bean.BizCompCollege;
import com.nmf.dao.gen.bean.BizCompCollegeExample;
import com.nmf.dao.gen.bean.BizCompany;
import com.nmf.dao.gen.bean.BizCompanyExample;
import com.nmf.site.basedata.category.enums.CategoryType;
import com.nmf.site.basedata.category.service.BaseBizCategoryService;
import com.nmf.site.basedata.college.service.BaseCollegeService;
import com.nmf.site.biz.comp.service.BizCompanyService;
import com.nmf.site.biz.compcollege.comp.bean.CompCollegeBean;
import com.nmf.site.biz.compcollege.comp.enums.CompTypeEnums;
import com.nmf.site.biz.compcollege.comp.enums.IsCheckedEnums;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;
@Component
public class CompCollegeService {
	private final static Logger log = LoggerFactory.getLogger(CompCollegeService.class);
	@Autowired
	BaseCollegeMapper baseCollegeMapper;
	@Autowired
	BaseCategoryMapper baseCategoryMapper;
	@Autowired
	BaseBizCategoryService baseBizCategoryService;
	@Autowired
	BizCompanyMapper bizCompanyMapper;
	@Autowired
	BizCompCollegeMapper bizCompCollegeMapper;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BaseCollegeService baseCollegeService;
	@Autowired
	BizCompanyService bizCompanyService;
	//znode 数据结构  左侧为公司 公司有分类 应该是分类底下挂 公司 map形式返回数据
	/**
	 * 获取 大学的树结构
	 * @return
	 */
	public Map<String,List<BaseCollege>> queryTreeCollegeList(){
		List<BaseCategory> bclist = baseBizCategoryService.queryCategory(CategoryType.company);
		
		BaseCollegeExample baseCollegeExample = new BaseCollegeExample();
		List<BaseCollege> collegeList = baseCollegeMapper.selectByExample(baseCollegeExample);
		
		List<BaseCollege> inList = new ArrayList<BaseCollege>();
		Map<String,List<BaseCollege>> dmap = new HashMap<>();
		
		for(BaseCategory baseCategory : bclist) {
			BaseCollegeExample baseCatCollegeExample = new BaseCollegeExample();
			BaseCollegeExample.Criteria criteria = baseCatCollegeExample.createCriteria();
			
			criteria.andCategoryIdEqualTo(baseCategory.getId());
			
			List<BaseCollege> blist = baseCollegeMapper.selectByExample(baseCatCollegeExample);
			
			inList.addAll(blist);
			dmap.put(baseCategory.getId(), blist);
		}
		
		collegeList.removeAll(inList);
		dmap.put("", collegeList);
		return dmap;
	}
	
	public Page<CompCollegeBean> findPage(Page<BaseCollege> page, BaseCollege baseCollege,String compId,CompTypeEnums compTypeEnums) {
		BaseCollegeExample baseCollegeExample = new BaseCollegeExample();
		BaseCollegeExample.Criteria criteria = baseCollegeExample.createCriteria();
		
		setQuery(criteria,baseCollege);
		baseCollegeExample.setPage(page);
		
		List<BaseCollege> dlist = baseCollegeMapper.selectByExample(baseCollegeExample);
		page.setList(dlist);
		page.setCount(baseCollegeMapper.countByExample(baseCollegeExample));
		
		Page<CompCollegeBean> ccpage = new Page<CompCollegeBean>();
		List<CompCollegeBean> ccBlist = new ArrayList<CompCollegeBean>();
		
		BizCompany bizCompany = bizCompanyMapper.selectByPrimaryKey(compId);
		for(BaseCollege baseDCollege : dlist) {
			CompCollegeBean compCollegeBean = new CompCollegeBean();
			compCollegeBean.setBaseCollege(baseDCollege);
			compCollegeBean.setBizCompany(bizCompany);
			
			IsCheckedEnums isCheckedEnums = queryIsChecked(baseDCollege.getId(),compId,compTypeEnums); 
			compCollegeBean.setIsChecked(isCheckedEnums.value);
			ccBlist.add(compCollegeBean);
		}
		
		
		BeanUtils.copyProperties(page, ccpage);
		ccpage.setList(ccBlist);
		ccpage.setCount(ccpage.getCount());
		return ccpage;
	}
	
	public IsCheckedEnums queryIsChecked(String collegeId,String compId,CompTypeEnums compTypeEnums) {
		BizCompCollege bizCompCollege = queryCompColl(collegeId,compId,compTypeEnums);
		if(bizCompCollege==null) {
			return IsCheckedEnums.unchecked;
		}
		IsCheckedEnums isCheckedEnums = IsCheckedEnums.getStatus(bizCompCollege.getIsChecked());
		if(IsCheckedEnums.checked.equals(isCheckedEnums)) {
			return IsCheckedEnums.checked;
		}else {
			return IsCheckedEnums.unchecked;
		}
	}
	public BizCompCollege queryCompColl(String collegeId,String compId,CompTypeEnums compTypeEnums) {
		BizCompCollegeExample bizCompCollegeExample = new BizCompCollegeExample();
		BizCompCollegeExample.Criteria criteria = bizCompCollegeExample.createCriteria();
		
		criteria.andCollegeIdEqualTo(collegeId);
		criteria.andCompanyIdEqualTo(compId);
		criteria.andTypeEqualTo(compTypeEnums.value);
		
		List<BizCompCollege> blist = bizCompCollegeMapper.selectByExample(bizCompCollegeExample);
		if(blist.size()>0) {
			return blist.get(0);
		}
		return null;
	}
	private void setQuery(BaseCollegeExample.Criteria criteria ,BaseCollege baseCollege) {
		if(StringUtils.isNotBlank(baseCollege.getCategoryId())) {
			criteria.andCategoryIdEqualTo(baseCollege.getCategoryId());
		}
		if(StringUtils.isNotBlank(baseCollege.getCollegeCode())) {
			criteria.andCollegeCodeEqualTo(baseCollege.getCollegeCode());
		}
		
		if(StringUtils.isNotBlank(baseCollege.getProvinceId())) {
			criteria.andProvinceIdEqualTo(baseCollege.getProvinceId());
		}
		
		if(StringUtils.isNotBlank(baseCollege.getLevel())) {
			criteria.andLevelEqualTo(baseCollege.getLevel());
		}
	}
	/**
	 * 获取 公司的树结构
	 * @return
	 */
	public Map<String,List<BizCompany>> queryTreeCompByCategory(){
		List<BaseCategory> bclist = baseBizCategoryService.queryCategory(CategoryType.company);
		
		BizCompanyExample bizCompanyExample = new BizCompanyExample();
		List<BizCompany> companyList = bizCompanyMapper.selectByExample(bizCompanyExample);
		
		List<BizCompany> inList = new ArrayList<BizCompany>();
		Map<String,List<BizCompany>> dmap = new HashMap<>();
		
		for(BaseCategory baseCategory : bclist) {
			BizCompanyExample bizCateCompanyExample = new BizCompanyExample();
			BizCompanyExample.Criteria criteria = bizCateCompanyExample.createCriteria();
			
			criteria.andCategoryIdEqualTo(baseCategory.getId());
			
			List<BizCompany> blist = bizCompanyMapper.selectByExample(bizCateCompanyExample);
			
			inList.addAll(blist);
			dmap.put(baseCategory.getId(), blist);
		}
		
		removeBizComp(companyList,inList);
		dmap.put("", companyList);
		return dmap;
	}
	
	public void removeBizComp(List<BizCompany> companyList,List<BizCompany> inList) {
		Map<String,BizCompany> allMap = new HashMap<String,BizCompany>();
		for(BizCompany bizCompany : companyList) {
			allMap.put(bizCompany.getId(), bizCompany);
		}
		
		for(BizCompany bizCompany : inList) {
			if(allMap.get(bizCompany.getId())!=null) {
				companyList.remove(allMap.get(bizCompany.getId()));
			}
		}
	} 
	@Transactional
	public void saveCompCollege(String[] compChecked,String[] compIds,CompTypeEnums compTypeEnums) {
		Set<String> collegeIdList = Sets.newHashSet(compChecked);
		Set<String> compIdList = Sets.newHashSet(compIds);
		Date date = new Date();
		for(String compId : compIdList) {
			for(String collegeId : collegeIdList) {
				BizCompCollege bizCompCollege = queryCompColl(collegeId,compId,compTypeEnums);
				BizCompCollege bizCompCollegeRecord = new BizCompCollege();
				
				if(bizCompCollege==null) {
					BaseCollege baseCollege = baseCollegeService.get(collegeId);
					BizCompany bizCompany = bizCompanyService.get(compId);
					bizCompCollegeRecord.setCollegeId(collegeId);
					bizCompCollegeRecord.setCollegeName(baseCollege.getCollegeName());
					bizCompCollegeRecord.setCompanyId(compId);
					bizCompCollegeRecord.setCompanyName(bizCompany.getCompName());
					bizCompCollegeRecord.setCreateDate(date);
					bizCompCollegeRecord.setId(getCmpNo());
					bizCompCollegeRecord.setIsChecked(IsCheckedEnums.checked.value);
					bizCompCollegeRecord.setType(compTypeEnums.value);
					bizCompCollegeRecord.setUpdateDate(date);
					int optNum = bizCompCollegeMapper.insertSelective(bizCompCollegeRecord);
					if(optNum==1) {
						log.info("插入数据学校【"+collegeId+"】公司【"+compId+"】成功");
					}
				}else {
					BeanUtils.copyProperties(bizCompCollege, bizCompCollegeRecord);
					bizCompCollegeRecord.setIsChecked(IsCheckedEnums.checked.value);
					int optNum = bizCompCollegeMapper.updateByPrimaryKeySelective(bizCompCollegeRecord);
					if(optNum==1) {
						log.info("更新数据学校【"+collegeId+"】公司【"+compId+"】成功");
					}
				}
			}
		}
	}
	
	private String getCmpNo() {
		return idGenerater.genClsNo("companyCollege");
	}
	
}
